package com.aaa.cars.controller;

import com.aaa.cars.entity.CarComment;
import com.aaa.cars.service.CarCommentService;
import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (CarComment)表控制层
 *
 * @author makejava
 * @since 2020-11-17 18:48:51
 */
@RestController
@RequestMapping("carComment")
public class CarCommentController {
    /**
     * 服务对象
     */
    @Resource
    private CarCommentService carCommentService;
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Result selectOne(Integer id) {
        //当ID为空时抛出自定义异常
        if (null==id){
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),this.carCommentService.queryById(id));
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @GetMapping("queryAllByLimit")
    public Result queryAllByLimit(int offset, int limit){
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),this.carCommentService.queryAllByLimit(offset,limit));
    };

    /**
     * 新增数据
     *
     * @param carComment 实例对象
     * @return 实例对象
     */
    @PostMapping("insert")
    public Result insert(@RequestBody CarComment carComment){
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),this.carCommentService.insert(carComment));
    };

    /**
     * 修改数据
     *
     * @param carComment 实例对象
     * @return 实例对象
     */
    @PutMapping("update")
    public Result update(@RequestBody CarComment carComment){
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),this.carCommentService.update(carComment));
    };

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @DeleteMapping("deleteById")
   public Result deleteById(Integer id){
        //当ID为空时抛出自定义异常
        if (id==null){
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
       return new Result(ReturnCode.SUCCESSS.getCode(),
               ReturnCode.SUCCESSS.getMsg(),this.carCommentService.deleteById(id));
   };

    /**
     * 分页和模糊查询
     * @param pageNo
     * @param pageSize
     * @param carComment
     * @return
     */
    @GetMapping("queryPageByParam")
    public Result queryPageByParam( Integer pageNo, Integer pageSize, CarComment carComment){
        return carCommentService.queryPageByParam(pageNo,pageSize,carComment);
    }


}